package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 作者:hfj
 * 功能:
 * 日期: 2024/4/18 19:47
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    /**
     *
     * @param current 当前页
     * @param limit 每页记录数
     * @param adminQueryVo 角色实体类
     * @return 用户列表
     */
    @ApiOperation("用户列表")
    @GetMapping("/{current}/{limit}")
    public Result list(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo){
        //1.创建page对象,传递当前页和每页记录数
        Page<Admin> pageParam = new Page<>(current,limit);

        //2.调用service的方法实现条件分页查询,返回分页对象
        IPage<Admin> pageModel = adminService.selectPageUser(pageParam,adminQueryVo);
        return Result.ok(pageModel);
    }

    /**
     *
     * @param id 用户id
     * @return 根据id查询
     */
    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    /**
     *
     * @param admin 用户实体类
     * @return 添加用户
     */
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin){
        //获取输入的密码
        String password = admin.getPassword();

        //md5加密处理
        String password_MD5 = MD5.encrypt(password);

        //保存回去
        admin.setPassword(password_MD5);

        boolean is_success = adminService.save(admin);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param admin 用户实体类
     * @return 修改用户
     */
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin){
        boolean is_success = adminService.updateById(admin);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param id 角色id
     * @return 根据id删除用户
     */
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = adminService.removeById(id);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param idList 批量删除的id
     * @return 批量删除
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean is_success = adminService.removeByIds(idList);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param adminId 用户id
     * @return 获取所有角色 根据用户查询用户分配角色列表
     */
    @ApiOperation("获取用户角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        //返回map：所有角色 和 为用户分配角色列表
        Map<String, Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    /**
     *
     * @param adminId 用户id
     * @param roleId 多个角色id
     * @return 为用户进行角色分配
     */
    @ApiOperation("为用户进行角色分配")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);
    }
}
